<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "UserJob".
 */
class UserJob extends ActiveRecord {

    const NatureFullTime = 0;
    const NaturePart = 1;
    public static $natureSelector = [
        self::NatureFullTime => ['title' => '全职', 'status' => 'green'],
        self::NaturePart => ['title' => '兼职', 'status' => 'red'],
    ];

    // only define rules for those attributes that
    // will receive user inputs.
    public function rules()
    {
        return [
            [['user_id', 'company', 'begin', 'finish', 'post', 'nature', 'website', 'category_id', 'describe'], 'required'],
            [['user_id', 'nature'], 'integer'],
            [['company', 'post'], 'string', 'max' => 64],
            [['begin', 'finish'], 'string', 'max' => 16],
            [['category_id'], 'string', 'max' => 32],
            [['website'], 'string', 'max' => 255],
            [['describe'], 'string', 'max' => 1024],
        ];
    }
    /**
     * @name 字段名称
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'user_id' => '用户',
            'company' => '公司名称',
            'begin' => '入职时间',
            'finish' => '离职时间',
            'post' => '岗位',
            'nature' => '工作性质',
            'website' => '公司网址',
            'category_id' => '行业分类',
            'describe' => '工作描述',
        ];
    }
    /**
     * @name update & insert data check config for html
     * @param $type string 页面操作类型
     * @param $encodeJson boolean 是否转成json串
     * @return string / array
     */
    public static function flyer($type = 'update')
    {
        // jsut search
        $rule = [
            'param' => [
                'company' => ['公司名称',['maxlength' => 64, 'required']],
                'begin' => ['入职时间',['date' => 'Y-m-d', 'required']],
                'finish' => ['离职时间',['date' => 'Y-m-d', 'required']],
                'post' => ['岗位',['maxlength' => 64, 'required']],
                'nature' => ['性质',['inkey' => static::$natureSelector, 'required']],
                'website' => ['网址',['url','required']],
                'category_id' => ['行业分类',['int', 'required']],
                'describe' => ['工作描述',['maxlength' => 1024, 'required']],
            ],
        ];
        return $rule;
    }

    /**
     * @name 获取user信息
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }
}